import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt

# 设置headers，模拟浏览器请求
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

# 获取数据函数
def get_data(page):
    url = f'https://www.amazon.com/best-sellers-books-Amazon/zgbs/books/ref=zg_bs_pg_{page}?_encoding=UTF8&pg={page}'
    html = requests.get(url, headers=headers).text
    soup = BeautifulSoup(html, 'html.parser')
    books = soup.find_all('div', {'class': 'a-section a-spacing-none aok-relative'})

    data = []

    for book in books:
        # 提取书籍名称
        name = book.find('span', {'class': 'zg-text-center-align'}).get_text().strip()

        # 提取书籍作者
        author = book.find('a', {'class': 'a-size-small a-link-child'}).get_text().strip()

        # 提取书籍价格
        price = book.find('span', {'class': 'p13n-sc-price'}).get_text().strip()

        # 提取书籍评分
        rating = book.find('span', {'class': 'a-icon-alt'}).get_text().strip()

        data.append([name, author, price, rating])

    return data

# 获取前5页的数据
data = []
for page in range(1, 6):
    data += get_data(page)

# 将数据转化为DataFrame
df = pd.DataFrame(data, columns=['Name', 'Author', 'Price', 'Rating'])

# 输出前5行数据
print(df.head())

# 统计评分分布
df['Rating'] = df['Rating'].str.split(' ').str[0].astype(float)
plt.hist(df['Rating'], bins=10, range=(0, 5))
plt.title('Rating Distribution')
plt.xlabel('Rating')
plt.ylabel('Count')
plt.show()

# 统计价格分布
df['Price'] = df['Price'].str.split('$').str[1].astype(float)
plt.hist(df['Price'], bins=10, range=(0, 50))
plt.title('Price Distribution')
plt.xlabel('Price')
plt.ylabel('Count')
plt.show()

# 输出平均价格和平均评分
print('Average Price:', df['Price'].mean())
print('Average Rating:', df['Rating'].mean())
